package com.imti.mall.sale.mapper;

import com.imti.mall.model.pms.pojo.PmsProduct;
import com.imti.mall.model.sms.pojo.SmsFlashPromotion;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * ClassName: SmsFlashPromotionDynaSqlProvider
 * Description:
 * date: 2022/9/19 23:23
 *
 * @author: youhun
 * @version:
 * @since JDK 1.8
 */
public class SmsFlashPromotionDynaSqlProvider {

    /**
     * 查询限时购表
     * @param smsFlashPromotion
     * @return
     */
    public String findSmsFlashPromotion(final SmsFlashPromotion smsFlashPromotion){

        return new SQL(){
            {
                SELECT(" id,title,start_date,end_date,status,create_time ");
                FROM(" sms_flash_promotion ");
                StringBuffer sb=new StringBuffer();
                if(smsFlashPromotion!=null) {

                    if (smsFlashPromotion.getTitle() != null) {
                        sb.append(" and  title  LIKE concat('%',#{title},'%')");
                    }
                    if (smsFlashPromotion.getStartDate() != null) {
                        sb.append(" and  start_date>=#{startDate}");
                    }
                    if (smsFlashPromotion.getStatus() != null) {
                        sb.append(" and  status=#{status}");
                    }
                    if (smsFlashPromotion.getEndDate() != null) {
                        sb.append(" and  end_date<=#{endDate}");
                    }

                    if (smsFlashPromotion.getCreateTime() != null) {
                        sb.append(" and  create_time=#{createTime}");
                    }
                }
                WHERE(" 1=1 "+sb.toString());

            }
        }.toString();

    }


    /**
     * 查询限时购表总条数
     * @param smsFlashPromotion
     * @return
     */
    public String countSmsFlashPromotion(final SmsFlashPromotion smsFlashPromotion){

        return new SQL(){
            {
                SELECT("  COUNT(*) ");
                FROM(" sms_flash_promotion ");
                StringBuffer sb=new StringBuffer();
                if(smsFlashPromotion!=null) {

                    if (smsFlashPromotion.getTitle() != null) {
                        sb.append(" and  title=#{title}");
                    }
                    if (smsFlashPromotion.getStartDate() != null) {
                        sb.append(" and  start_date>=#{startDate}");
                    }
                    if (smsFlashPromotion.getStatus() != null) {
                        sb.append(" and  status=#{status}");
                    }
                    if (smsFlashPromotion.getEndDate() != null) {
                        sb.append(" and  end_date<=#{endDate}");
                    }

                    if (smsFlashPromotion.getCreateTime() != null) {
                        sb.append(" and  create_time=#{createTime}");
                    }
                }
                WHERE(" 1=1 "+sb.toString());

            }
        }.toString();

    }


    /**
     * 根据id修改限时购
     * @param smsFlashPromotion
     * @return
     */
    public String updateSmsFlashPromotion(final SmsFlashPromotion smsFlashPromotion){

        return new SQL(){
            {
                UPDATE(" sms_flash_promotion ");


                if(smsFlashPromotion!=null) {

                    if (smsFlashPromotion.getTitle() != null) {
                        SET(" title=#{title}");
                    }
                    if (smsFlashPromotion.getStartDate() != null) {
                        SET(" start_date=#{startDate}");
                    }
                    if (smsFlashPromotion.getStatus() != null) {
                        SET(" status=#{status}");
                    }
                    if (smsFlashPromotion.getEndDate() != null) {
                        SET(" end_date=#{endDate}");
                    }

                    if (smsFlashPromotion.getCreateTime() != null) {
                        SET(" create_time=#{createTime}");
                    }
                }
                WHERE(" id=#{id} ");

            }
        }.toString();

    }
}
